
package asedra.huyle.helper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public final class DatabaseHelper extends SQLiteOpenHelper {
	
    private static final int DATABASE_VERSION = 1;
    public static final String DATABASE_EXTENSION = ".sqlite";
    public static final String DATABASE_NAME = "funcard" + DATABASE_EXTENSION;
    public static final String TEMP_PREFIX = "tmp_db_";
    
    public static final String CREATE_MATCH_STATEMENTS = "" +
    		"CREATE TABLE \"match\" ( "+
    		"	\"m_id\"        INTEGER PRIMARY KEY ASC AUTOINCREMENT,"+
    		"	\"date\"        DATE,"+
    		"	\"description\" VARCHAR,"+
    		"	\"title\"       VARCHAR "+
    		")";
    public static final String CREATE_PLAYER_STATEMENTS = "" +
    		"CREATE TABLE \"player\" ( "+
    		"	\"name\"   VARCHAR,"+
    		"	\"p_id\"   INTEGER PRIMARY KEY ASC AUTOINCREMENT,"+
    		"	\"avatar\" VARCHAR "+
    		"	)";
    public static final String CREATE_PLAYER_MATCH_STATEMENTS = "" +
    		"CREATE TABLE \"player_match\" ( "+
    		"	\"p_id\" INTEGER,"+
    		"	\"m_id\" INTEGER,"+
    		"	PRIMARY KEY ( m_id, p_id, turn_order ) "+
    		"	)";
    public static final String CREATE_RECORD_STATEMENTS = "" +
    		"CREATE TABLE \"record\" ( "+
    		"	\"m_id\"       INTEGER,"+
    		"	\"p_id\"       INTEGER,"+
    		"	\"turn_order\" INTEGER,"+
    		"	\"score\"      INTEGER,"+
    		"	PRIMARY KEY ( m_id, p_id ) "+
    		"	)"+
    		"";
    
    public static DatabaseHelper getInstance(Context context, String databaseName) {
        return new DatabaseHelper(context, databaseName);
    }
    
    private DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    
    private DatabaseHelper(Context context, String databaseName) {
        super(context, databaseName, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    	db.execSQL(CREATE_MATCH_STATEMENTS);
    	db.execSQL(CREATE_PLAYER_STATEMENTS);
    	db.execSQL(CREATE_PLAYER_MATCH_STATEMENTS);
    	db.execSQL(CREATE_RECORD_STATEMENTS);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}
